
package com.yd.controller.app;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Kefus;
import com.yd.service.KefusService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;
import com.yd.tool.UtsUuid;

//用户修改自己的资料
@Controller
public class KefusControllerApp {

	@Resource
	private KefusService kefusService;
  
	//客服登录
	@RequestMapping(value = "/app/kefu/login")
	@ResponseBody
	public String app_login(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		//获取参数
		String loginname="";
		String loginpass="";
		//验证参数
		if (UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "账号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass = request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		//通过账号查询此条数据
		Kefus kefus=kefusService.selectOne(new EntityWrapper<Kefus>().eq("loginname", loginname));
		//判断此账号是否有数据
		if(kefus==null){//如果没有数据 那说明不存在此账号
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//走到这里说明有账号，那么要判断这个账号的密码是否正确，传过来的密码要加密后再对比
		if(!kefus.getLoginpass().equals(MD5Hash.getMd5HashPasswod(loginpass))){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		kefus.setPhone(loginname);
		//生成一个随机数当作token
		kefus.setToken(UtsUuid.getUuid());
		//把这个token更新到数据库，每次登陆都要重新生成一次
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//把实体类里面的密码清空 然后返回给前台(所有返回用户信息的时候都要清空重要的信息，比如密码，身份证，等等)
		kefus.setLoginpass("");
		jsonObject.put("data", kefus);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//根据token获取用户信息
	@RequestMapping(value = "/app/kefu/token")
	@ResponseBody
	public String app_kefu_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Kefus kefus=kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "该客服不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		kefus.setLoginpass("");
		jsonObject.put("data", kefus);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//进入个人中心
	@RequestMapping(value = "/app/kefu/personal")
	@ResponseBody
	public String app_kefu_personal(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "token错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		EntityWrapper<Kefus> ew = new EntityWrapper<>();
		kefus = kefusService.selectOne(ew.eq("token", token));
		
		jsonObject.put("data", kefus);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
		
	}
	
	//修改密码
	@RequestMapping("/app/kefu/pass/up")
	@ResponseBody
	public String app_kefu_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String ypass = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("ypass"))){ypass=request.getParameter("ypass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Kefus kefus = kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(ypass).equals(kefus.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		kefus.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!kefusService.updateById(kefus)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	//修改头像
	@RequestMapping("/app/kefu/touxiang/up")
	@ResponseBody
	public String app_kefu_touxiang_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String img = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		kefus.setImg(img);
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//修改性别
	@RequestMapping("/app/kefu/sex/up")
	@ResponseBody
	public String app_kefu_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String sex = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		if ("男".equals(sex)) {
			kefus.setSex(sex);
		}else if("女".equals(sex)){
			kefus.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//修改年龄
	@RequestMapping("/app/kefu/age/up")
	@ResponseBody
	public String app_kefu_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String age = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("age", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		kefus.setAge(age);
		if(!kefusService.updateById(kefus)){
			jsonObject.put("age", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//修改昵称
	@RequestMapping("/app/kefu/nicheng/up")
	@ResponseBody
	public String app_kefu_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String nicheng = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		kefus.setNicheng(nicheng);
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//修改姓名
	@RequestMapping("/app/kefu/name/up")
	@ResponseBody
	public String app_kefu_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String name = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "name格式 错误,必须是1-5个汉字");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		kefus.setName(name);
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//修改邮箱
	@RequestMapping("/app/kefu/email/up")
	@ResponseBody
	public String app_kefu_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String email = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Kefus kefus =kefusService.selectOne(new EntityWrapper<Kefus>().eq("token", token));
		if(kefus==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		kefus.setEmail(email);
		if(!kefusService.updateById(kefus)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//客服密码找回
	@RequestMapping(value={"/app/kefu/psw/find"})
	@ResponseBody
	public String app_merchant_psw_find(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String loginname="";
		String nloginpass="";
		String nloginpass2="";
		if(UtsReg.getIntXl(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "用户名格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("nloginpass"))){nloginpass=request.getParameter("nloginpass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("nloginpass2"))){nloginpass2=request.getParameter("nloginpass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		Kefus kefus = kefusService.selectOne(new EntityWrapper<Kefus>().eq("loginname", loginname));
		if(kefus==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "该用户不存在");return jsonObject.toString();
		}
		if(!nloginpass.equals(nloginpass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		
		kefus.setLoginpass(MD5Hash.getMd5HashPasswod(nloginpass2));
		
		if(!kefusService.updateById(kefus)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
}
